package org.goochasca

import scala.collection.mutable.MutableList

private[goochasca] trait Axis extends Chart {
  
  private val list: MutableList[(Position, Seq[_])] = new MutableList
  
  def addAxisLabels(axis: Position, labels: Any*) {
    val axisLabels: Seq[_] = if (labels.isEmpty && (axis == RIGHT || axis == LEFT)) {
        range
    } else {
        labels
    }
    list += ((axis, labels))
  }

  protected abstract override def appendParms(sb: StringBuilder) {
    super.appendParms(sb)
    if (list.isEmpty) return
    
    val chxl = new StringBuilder
    var first = true
    var idx = -1
    for (entry <- list) {
      idx += 1
      if (first) {
        sb append "&chxt="
        chxl append "&chxl="
        first = false
      } else {
        sb append ','
      }
      sb append entry._1.axis
      chxl append idx append ":|"
      for (label <- entry._2) {
          chxl append label append '|'
      }
    }
    chxl setLength chxl.length-1
    sb append chxl
  }
}
